<?php namespace App\Http\Controllers\Api;

use App\Answers;
use App\Http\Controllers\Controller;
use App\Questions;
use Illuminate\Http\Request;

class QuestionController extends Controller
{
    public function checkAnswer(Request $request){
        $aid = $request->get('aid');
        /** @var Answers $thatAnswer */
        $thatAnswer = Answers::whereId($aid)->first();
        if(!$thatAnswer)
            return response()->json(['level'=>'error','message'=>'该答案不存在哦！']);

        $thatRightAnswer = Answers::whereQid($thatAnswer->qid)->whereIsRight(1)->first();
        $thatQuestion = Questions::whereId($thatAnswer->qid)->first();

        $result['answer']  = $thatRightAnswer->select_num;
        $result['answer_id'] = $thatRightAnswer->id;
        $result['analyse'] = $thatQuestion->analyse;
        $result['qid']     = $thatAnswer->qid;

        if($thatAnswer->is_right == 0){
            return response()->json(['level'=>'no_right','message'=>'答案不正确哦！','data'=>$result]);
        }else{
            return response()->json(['level'=>'right','message'=>'回答正确！','data'=>$result]);
        }
    }

    public function setAnswer(Request $request){
        $selectNum = $request->get('select_num');
        $answerContent    = $request->get('answer');
        $isRight   = $request->get('is_right');

        $answer = new Answers();
        $answer->select_num = $selectNum;
        $answer->answer     = $answerContent;
        $answer->is_right   = $isRight;
        $answer->save();

        return response()->json(['level'=>'success','message'=>'添加成功','data'=>$answer]);
    }

    public function setQuestion(Request $request){
        $questionInfo = $request->get('question');
        $answerIds    = $request->get('answer_ids');
        $content      = $request->get('content');
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatQuestion = Questions::whereId($id)->first();

        if(!$thatQuestion){
            $thatQuestion = new Questions();
            $thatQuestion->question = $questionInfo;
            $thatQuestion->analyse  = $content;
            $thatQuestion->save();

            $answerIdsArry = explode(',',$answerIds);
            $thatAnswers = Answers::whereIn('id',$answerIdsArry)->get();
            if(count($thatAnswers) > 0){
                foreach ($thatAnswers as $answer){
                    /** @var Answers $answer */
                    $answer->qid = $thatQuestion->id;
                    $answer->save();
                }
            }
        }else{
            $thatQuestion->question = $questionInfo;
            $thatQuestion->analyse  = $content;
            $thatQuestion->save();
        }


        return response()->json(['level'=>'success','message'=>'添加或修改成功','data'=>$thatQuestion]);
    }

    public function getQuestion(){
        $allQuestions = Questions::all();

        $questionArry = array();
        foreach ($allQuestions as $question) {
            array_push($questionArry, $question);
        }
        $lenth = count($allQuestions);
        $num = -1;
        if ($lenth > 0) {
            $num = mt_rand(0, $lenth - 1);
        }

        $result = null;
        if ($num != -1) {
            $result = $questionArry[$num];
        }

        $answers = null;
        if(!is_null($result)){
            $thatAnswers = Answers::whereQid($result['id'])->get();
            $answers = $thatAnswers;
        }

        $q["question"] = $result;
        $q["answers"]  = $answers;

       return response()->json(["level"=>"success","message"=>"获取成功","data"=>$q]);
    }

    public function changeAnswer(Request $request){
        $aid = $request->get("aid");
        $selectNum = $request->get('select_num');
        $isRight   = $request->get("is_right");
        $answer    = $request->get("answer");

        /** @var Answers $thatAnswer */
        $thatAnswer = Answers::whereId($aid)->first();

        if(!$thatAnswer)
            return response()->json(["level"=>"error","message"=>"当前选项不存在"]);

        $thatAnswer->select_num = $selectNum;
        $thatAnswer->is_right   = $isRight;
        $thatAnswer->answer     = $answer;
        $thatAnswer->save();

        return response()->json(["level"=>"success","message"=>"设置完成"]);
    }

    public function delAnswer(Request $request){
        $aid = $request->get("aid");

        /** @var Answers $thatAnswer */
        $thatAnswer = Answers::whereId($aid)->first();

        if(!$thatAnswer)
            return response()->json(["level"=>"error","message"=>"当前选项不存在"]);

        $thatAnswer->delete();

        return response()->json(["level"=>"success","message"=>"删除成功"]);
    }

    public function delQuestion(Request $request){
        $qid = $request->get("qid");

        /** @var Answers $thatQuestion */
        $thatQuestion = Questions::whereId($qid)->first();

        if(!$thatQuestion)
            return response()->json(["level"=>"error","message"=>"当前选项不存在"]);

        $thatQuestion->delete();

        return response()->json(["level"=>"success","message"=>"删除成功"]);
    }
}